package com.spa.infrastructure.repository.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spa.domain.entity.Refund;
import com.spa.infrastructure.mapper.RefundMapperStruct;
import com.spa.infrastructure.repository.condition.RefundCondition;
import com.spa.infrastructure.repository.mapper.RefundMapper;
import com.spa.infrastructure.repository.po.RefundPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class RefundRepositoryService extends ServiceImpl<RefundMapper, RefundPO> {

    @Autowired
    private RefundMapper refundMapper;

    public Long add(RefundPO refundPO){
        refundMapper.insert(refundPO);
        return refundPO.getId();
    }

    public List<Refund> list(RefundCondition condition) {
        LambdaQueryWrapper<RefundPO> wrapper = wrapper(condition);
        List<RefundPO> pos = refundMapper.selectList(wrapper);
        return RefundMapperStruct.INSTANCE.po2EntityList(pos);
    }


    private LambdaQueryWrapper<RefundPO> wrapper(RefundCondition condition) {
        LambdaQueryWrapper<RefundPO> wrapper = Wrappers.lambdaQuery();
        wrapper.in(CollUtil.isNotEmpty(condition.getOrderCodes()), RefundPO::getOutTradeNo, condition.getOrderCodes());
        return wrapper;
    }

}
